package com.topscore.uke.ui.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.entities.MemberEntity;
import com.topscore.uke.ui.BaseSupportFragment;
import com.topscore.uke.ui.activities.MemberBasicInfoActivity;
import com.topscore.uke.utils.DisplayImageOptionsManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class MemberListSearchResultFragment extends BaseSupportFragment {


    @Bind(R.id.search_recycler)
    RecyclerView searchRecycler;

    private View rootView;
    private View emptyView;

    private List<MemberEntity> mMemberList, mSearchResult;

    private BaseQuickAdapter<MemberEntity, BaseViewHolder> mAdapter;

    public MemberListSearchResultFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_member_list_search_result, container, false);
        ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    private void initViews() {
        initAdapter();
        searchRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        emptyView = LayoutInflater.from(mContext).inflate(R.layout.search_result_empty, (ViewGroup) searchRecycler.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        searchRecycler.setAdapter(mAdapter);
    }

    private void initAdapter() {
        mAdapter = new BaseQuickAdapter<MemberEntity, BaseViewHolder>(R.layout.member_search_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder viewHolder, MemberEntity entity) {
                viewHolder.setText(R.id.name_tv, entity.getMemberName())
                        .setText(R.id.phone_tv, entity.getPhone());
                ImageLoader.getInstance().displayImage(entity.getPortrait(),
                        (ImageView) viewHolder.getView(R.id.avatar_iv),
                        DisplayImageOptionsManager.getInstance().getAvatarOptions());
                viewHolder.getConvertView().setOnClickListener(v -> {
                    Intent intent = new Intent(getActivity(), MemberBasicInfoActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("memberName", entity.getMemberName());
                    bundle.putString("memberID", entity.getMemberID());
                    bundle.putString("phone", entity.getPhone());
                    intent.putExtras(bundle);
                    startActivity(intent);
                });
            }
        };
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void setMemberList(List<MemberEntity> memberList) {
        this.mMemberList = memberList;
    }

    public void searchMember(CharSequence searchWord) {
        mSearchResult = new ArrayList<>();
        for (int i = 0; i < mMemberList.size(); i++) {
            MemberEntity entity = mMemberList.get(i);
            if (entity.getMemberName().contains(searchWord)) {
                mSearchResult.add(entity);
            }
        }
        mAdapter.setNewData(mSearchResult);
    }
}
